package com.engine.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @author FY
 * @version 1.0.0
 * @ClassName CacheConfig.java
 * @Description TODO 缓存配置类，用于生成缓存对象，支撑矢量切片、栅格数据的缓存
 * @createTime 2021年10月20日
 */
@Configuration
@EnableCaching
public class CacheConfig {


    @Primary
    @Bean("vectorTilesCache")
    public CacheManager vectorTilesCache() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .maximumSize(10000)
                .expireAfterWrite(3, TimeUnit.DAYS)
                .initialCapacity(1000));
        return cacheManager;
    }

    @Bean("geoJsonCache")
    public CacheManager geoJsonCache() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .maximumSize(10000)
                .expireAfterWrite(3, TimeUnit.DAYS)
                .initialCapacity(1000));
        return cacheManager;
    }

    @Bean("locationCache")
    public CacheManager locationCache() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .maximumSize(10000)
                .expireAfterWrite(3, TimeUnit.DAYS)
                .initialCapacity(1000));
        return cacheManager;
    }

    @Bean("dataCache")
    public CacheManager dataCache() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .maximumSize(10000)
                .expireAfterWrite(3, TimeUnit.DAYS)
                .initialCapacity(1000));
        return cacheManager;
    }

    @Bean("msgExchangeCache")
    public CacheManager msgExchangeCache() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(3000, TimeUnit.SECONDS)
                .initialCapacity(100));
        return cacheManager;
    }

}
